<?php
/**
 * author: Kirk Amoah
 * date:4/21/2014
 * description: An action page that carries out all commands
 */
	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd){
		case 1:
			//get one district based on id
			get_all_id();
			break;
		case 2:
			//get all districts
			get_all_district();
			break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
			
		case 3:
		//edit a district detail
		update_district();
			break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
			
		case 4:
		//delete a district
		delete_district();
			break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";

	case 5:
		//add a district
		add_district();
			break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
			
	case 6:
		//perform a search through the district table
		search_district();
			break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
	}
	
	
	function get_all_id(){
		include_once("district_class.php");
		
			$id=get_datan("id");
			$v=new district();
			$res=$v->get_all_id($id);
			$row=$v->fetch($res);
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","district not found");
			echo "}";
			return;
		}
		
		echo "{";
			echo jsonn("result",1) .",";
			echo '"district":{';
			echo jsonn("id",$id).",";
			
			echo jsons("district_name",$row['district_name']) .",";
			
			echo jsons("district_capital",$row['district_capital']) .",";
			
			echo jsons("region_id",$row['region_id']);
			echo "}";
		echo "}";
	}
	
	function get_all_district(){
		include_once("district_class.php");
			$v=new district();
			$v->get_all_district();
			$row=$v->fetch();
		if(!$row){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","district not found");
			echo "}";
			return;
		}
		echo "{";
		echo jsonn("result",1) .",";
		echo '"district":';
		$val = Array(); 
		
		do { 
			array_push($val, $row);
			$row =$v->fetch();   
		}while ($row);	 
		print_r(json_encode($val));
		echo "}";
	}
	
	function update_district(){
		$id=get_datan('id');
		$district_name=get_data('dn');
		$district_capital=get_data('dc');
		$region_id=get_datan('rd');
		
		if (!$id){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","id not found");
			echo "}";
			return;
		}
		include_once("district_class.php");
			$v=new district();
			$valid=$v->update_district($id,$district_name,$district_capital,$region_id);
		if($valid){
			echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","district updated");
			echo "}";
			}
      else{
          echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","district not updated");
			echo "}";    	  
		}
	}
	
	function delete_district(){
	include_once("district_class.php");
		$v=new district();
		$id=get_datan('id');
	if (!$id){
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","id not found");
			echo "}";
			return;
		}
	if(!$v->delete_district($id)){
	
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","district not deleted");
			echo "}";
			
		}
      else{
          echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","district  deleted");
			echo "}";    	  
		}
	}
	
	function add_district(){
	include_once("district_class.php");
		$v=new district();
		$district_name=get_data('dn');
		$district_capital=get_data('dc');
		$region_id=get_datan('rd');
	$valid=$v->add_district($district_name,$district_capital,$region_id);
	if($valid){
			echo "{";
			echo jsonn("result",1). ",";
			echo jsons("message","district added");
			echo "}";
			}
			
      else{
          echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","district not added");
			echo "}";
		}
	}
	
	function search_district(){
	include_once("district_class.php");
		$v= new district();
		$district_name=get_data('dn');
		$row=$v->search_district($district_name);
	
	
		if(!$row){
		echo "{";
		 
			echo jsonn("result",0). ",";
			echo jsons("message","district not found");
			echo "}";    
			return;
			}
			echo "{";
		echo jsonn("result",1). ",";
			echo '"district":';
			$val = Array(); 	
			do { 
			array_push($val, $row);
			$row =$v->fetch();   
		}while ($row);	 
		print_r(json_encode($val));
		echo "}";
}
			
      
         
	

	
?>